﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;

namespace JunQuanPrinter
{
    class CCommSerial
    {
        private CSerial m_Serial = new CSerial();
        private Boolean m_Opened = false;


        /// <summary>
        /// 关闭串口
        /// </summary>
        public void Close()
        {
            m_Serial.closePort();
            m_Opened = false;
        }


        /// <summary>
        /// 打开串口
        /// </summary>
        /// <returns></returns>
        public Boolean Open()
        {
            m_Opened = m_Serial.openPort();
            return m_Opened;
        }


        /// <summary>
        /// 设置串口
        /// </summary>
        /// <param name="comPort"></param>
        /// <param name="Baud"></param>
        /// <returns></returns>
        public Boolean setParam(String comPort, Int32 Baud)
        {
            if(m_Opened)
            {
                Close();
            }
            m_Serial.setSerialPort(comPort, Baud, 8, 1);
            return true;
        }


        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="revBuf"></param>
        /// <returns></returns>
        public Int32 SendCommand(Byte[] buf, Int32 sendLen, Int32 OverTime, ref Byte[] revBuf)
        {
            Int32 ret = m_Serial.SendCommand(buf, sendLen, ref revBuf, OverTime);
            return ret;
        }


        public SerialPort getSerial()
        {
            return m_Serial.getPort();
        }


        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="OverTime"></param>
        /// <param name="revBuf"></param>
        /// <returns></returns>
        public Int32 ReadData(Int32 OverTime, ref Byte[] revBuf)
        {
            return m_Serial.ReadData(ref revBuf, OverTime);
        }


    }
}
